<?php

/**
 * @Author: king
 * @Date:   2019-05-04 17:30:00
 * @Last Modified by:   king
 * @Last Modified time: 2019-05-12 11:41:19
 */
namespace App\Models\Api;
use App\Exceptions\ApiException;
use App\Models\Api\Base;
use Cache;

/**课程评论踩或赞模型
 * Class LanmeiCourseComment
 * @package app\common\model
 */
class LanmeiCourseCommentSupport extends  Base
{

    protected $table = 'course_comment_support';

    const COMMENT_GOOD = 'course:comment:good:';//当前评论的点赞数
    const COMMENT_BAD = 'course:comment:bad:';//当前评论的踩数
    const CACHE_TIME = 86400*7;

    /**删除踩赞的缓存
     * @param array $map
     */
    public function rm_cache($map=[])
    {
        $id = $map['id'];//评论id
        Cache::forget(self::COMMENT_GOOD.$id);
        Cache::forget(self::COMMENT_BAD.$id);
    }

    /**获取当前评论的踩或赞的数量
     * @param array $map
     * @return array|bool|mixed
     */
    public function getCourseCommentBadOrGood($map=[])
    {
        $id = $map['id'];

        $type = $map['type'];//type=>1(踩)

        $num =false;

        switch ($type)
        {
            case 1:
                $cachename = self::COMMENT_BAD.$id;
                $where =[
                    'cid'=>$id,
                    'status'=>0
                ];
                break;
            case 2:
                $cachename = self::COMMENT_GOOD.$id;
                $where =[
                    'cid'=>$id,
                    'status'=>1
                ];
                break;
            default :
                $cachename ='';
                $where =[];
                break;
        }

        if ($this->cache_on>=1 && Cache::has($cachename))
        {
            $num = Cache::get($cachename);
        }

        if ($num ===false)
        {
           try{
               $num = $this
                   ->where($where)
                   ->distinct(true)
                   ->get(['uid']);

           }catch (\Exception $e)
           {
              throw new ApiException('获取评论的踩或赞的数量失败', config('code.error'));
           }

           Cache::set($cachename,$num,self::CACHE_TIME);
        }

        return $num;
    }


    /**添加踩或赞相关模型
     * @param int $uid 用户id
     * @param int $cid 评论id
     * @param int $type  1=>踩 2=>赞
     * @return bool|int|string
     */
    public function save_data($uid=0,$cid=0,$type=0)
    {
        $array=[
            'uid'=>$uid,
            'cid'=>$cid,
            'status'=>$type -1,
            'create_time'=>time()

        ];

        try{

            $id = $this->insertGetId($array);

            $this->rm_cache(['id'=>$cid]);

        }catch (\Exception $e)
        {
          throw new ApiException('操作失败', config('code.error'));
        }

        return $id;
    }


    /**更新踩或赞相关模型
     * @param int $uid
     * @param int $cid
     * @param int $type
     * @return bool
     */
    public function update_data($uid=0,$cid=0,$type=0)
    {
        $array=[
            'status'=>$type -1,
            'update_time'=>time()

        ];

        $where =[
            'cid'=>$cid,
            'uid'=>$uid
        ];

        try{

            $this::update($array,$where);

            $this->rm_cache(['id'=>$cid]);

        }catch (\Exception $e)
        {
             throw new ApiException('操作失败', config('code.error'));
        }

        return true;
    }


    /**删除踩或赞相关模型
     * @param int $uid
     * @param int $cid
     * @return bool
     */
    public function del_data($uid=0,$cid=0)
    {
        $where =[
            'cid'=>$cid,
            'uid'=>$uid
        ];

        try{

           $this->where($where)->delete();

            $this->rm_cache(['id'=>$cid]);

        }catch (\Exception $e)
        {
            throw new ApiException('操作失败', config('code.error'));
        }

        return true;
    }
}
